package org.evelsoft.jbpm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;

/**
 *
 * @author wojtek
 */
public class TestJBPM {

	protected static ProcessEngine processEngine = null;
	protected static RepositoryService repositoryService;
	protected static ExecutionService executionService;
	protected static ManagementService managementService;
	protected static TaskService taskService;
	protected static HistoryService historyService;
	protected static IdentityService identityService;
	protected List<String> registeredDeployments = new ArrayList<String>();
	String deploymentId;

	public TestJBPM() {
		System.out.println("test JBPM constructor");
	}

	public static void main(String[] args) {
		TestJBPM test = new TestJBPM();
		System.out.println("start");
		test.testJBPM();
		System.out.println("koniec");
	}

	protected void initialize() {
		if (processEngine == null) {
			System.out.println("Configuration.getProcessEngine();");
			processEngine = Configuration.getProcessEngine();
			System.out.println("processEngine.get(RepositoryService.class)");
			repositoryService = processEngine.get(RepositoryService.class);
			System.out.println("processEngine.getExecutionService();");
			executionService = processEngine.getExecutionService();
			System.out.println("processEngine.getHistoryService();");
			historyService = processEngine.getHistoryService();
			System.out.println("processEngine.getManagementService();");
			managementService = processEngine.getManagementService();
			System.out.println("processEngine.getTaskService();");
			taskService = processEngine.getTaskService();
			System.out.println(" processEngine.getIdentityService();");
			identityService = processEngine.getIdentityService();
			System.out.println("END inicjalization");
		}
	}

	public String testJBPM() {
		System.out.println("testJBPM start");
		initialize();
		String deploymentDbid = repositoryService.createDeployment().addResourceFromClasspath("myProcess.jpdl.xml").deploy();
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("content", "good");
		ProcessInstance processInstance = executionService.startProcessInstanceByKey("helloWorld", variables);
		System.out.println("Process reached state " + processInstance.getState());
		return "JBPM";
	}
}
